﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;

namespace Client
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 浏览获取对应的文件信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnExplorer_Click(object sender, EventArgs e)
        {
            OpenFileDialog dialog = new OpenFileDialog();
            dialog.Filter = "文本文件|*.txt|csv文件|*.csv";
            if (DialogResult.OK == dialog.ShowDialog(this))
            {
                txtPath.Text = dialog.FileName;
            }
        }
        private static void sortList(IList<CalcItem> source)
        {
            for (int i = 1; i < source.Count; i++)
            {
                CalcItem temp = source[i];
                double point = temp.Ponit;
                int j = i;
                while (j > 0 && source[j - 1].Ponit < point)
                {
                    source[j] = source[j - 1];
                    --j;
                }
                source[j] = temp;
            }
        }
        private void btnCalc_Click(object sender, EventArgs e)
        {
            if (!File.Exists(txtPath.Text))
            {
                MessageBox.Show("需要计算的文件不存在!");
                return;
            }
            CalcHelper helper = new CalcHelper();
            IList<CalcItem> list = helper.GetCalcItemList(txtPath.Text);
            double paramA = convert2Double(txtParamA.Text);
            double paramN = convert2Double(txtParamN.Text);
            double paramM = convert2Double(txtParamM.Text);
            if (paramA >= 1 || paramA == -1)
            {
                MessageBox.Show("参数A输入错误");
                return;
            }
            if (paramN < 0)
            {
                MessageBox.Show("参数N输入错误");
                return;
            }
            if (paramM < 0)
            {
                MessageBox.Show("参数M输入错误");
                return;
            }
            helper.Calc(list, paramA, paramN, paramM);
            sortList(list);
            writeList2csv(list);
        }

        double convert2Double(string param)
        {
            double result = -1;
            if (double.TryParse(param, out result))
            {
                return result;
            }
            return -1;
        }
        private void writeList2csv(IList<CalcItem> list)
        {
            SaveFileDialog dialog = new SaveFileDialog();
            dialog.Filter = "csv文件|*.csv";
            if (DialogResult.OK == dialog.ShowDialog())
            {
                using (StreamWriter writer = new StreamWriter(dialog.FileName, false, Encoding.GetEncoding("GBK")))
                {
                    foreach (var item in list)
                    {
                        writer.WriteLine("{0},{1},{2}", item.Name, item.Prize, item.Ponit);
                    }
                }
            }
        }
    }
}
